#! /usr/bin/php -q
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com)
*  (c) 2004-2006 Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * Cron tack for return mails analysis
 *
 * @author	Kasper Skaarhoj <kasperYYYY@typo3.com>
 * @author	Stanislas Rolland <stanislas.rolland(arobas)fructifor.ca>
 *
 * $Id: returnmail.phpsh 9476 2008-07-18 07:58:42Z ivankartolo $
 *
 */

define('TYPO3_MODE', 'BE');
define('TYPO3_cliMode', TRUE);

require __DIR__ . '/../../../../../typo3/sysext/core/Classes/Core/CliBootstrap.php';
\TYPO3\CMS\Core\Core\CliBootstrap::checkEnvironmentOrDie();

require __DIR__ . '/../../../../../typo3/sysext/core/Classes/Core/Bootstrap.php';
\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
	->baseSetup('typo3conf/ext/direct_mail/res/scripts/')
	->loadConfigurationAndInitialize()
	->loadTypo3LoadedExtAndExtLocalconf(TRUE)
	->applyAdditionalConfigurationSettings()
	->initializeTypo3DbGlobal();

// MAIL CONTENT
$filename = 'php://stdin';

$content = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($filename);
if (trim($content))	{
	$readMail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('DirectMailTeam\\DirectMail\\Readmail');

		// Split mail into head and content
	$mailParts = $readMail->extractMailHeader($content);
		// Find id
	$midArr = $readMail->find_XTypo3MID($content);
	if (!is_array($midArr))	{
		$midArr = $readMail->find_MIDfromReturnPath($mailParts['to']);
	}

		// Extract text content
	$c = trim($readMail->getMessage($mailParts));
	$cp = $readMail->analyseReturnError($c);

	$res = $TYPO3_DB->exec_SELECTquery('uid,email', 'sys_dmail_maillog', 'rid='.intval($midArr['rid']).' AND rtbl="'.$TYPO3_DB->quoteStr($midArr['rtbl'], 'sys_dmail_maillog').'" AND mid='.intval($midArr['mid']).' AND response_type=0');
	if (!$TYPO3_DB->sql_num_rows($res)) {
		$midArr = array();
		$cp = $mailParts;
	} else {
		$row = $TYPO3_DB->sql_fetch_assoc($res);
		$midArr['email'] = $row['email'];
	}

	$insertFields = array(
		'tstamp' => time(),
		'response_type' => -127,
		'mid' => intval($midArr['mid']),
		'rid' => intval($midArr['rid']),
		'email' => $midArr['email'],
		'rtbl' => $midArr['rtbl'],
		'return_content' => serialize($cp),
		'return_code' => intval($cp['reason'])
	);
	$TYPO3_DB->exec_INSERTquery('sys_dmail_maillog', $insertFields);
}

?>
